package com.matriq.cloudmanager;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.HttpMsg;
import net.lgpage.http.SwipeRefeshAsyncTask;
import net.lgpage.util.Checker.StringChecker;
import net.lgpage.util.Cleaner;

public class SuggestActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_suggest);
		setViews();
		setEvents();
		loadData();
	}

	Button bt_submit;
	TextView tv_title;
	EditText et_content;
	ImageButton ll_back;

	private void setViews() {
		bt_submit = (Button) this.findViewById(R.id.bt_submit);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		et_content = (EditText) this.findViewById(R.id.et_content);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
	}

	private void setEvents() {
		bt_submit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(checkEts()){
					putObj();
				}
			}
		});
	}

	Object copyObj;

	public boolean checkEts() {
		if (copyObj == null) {
			copyObj = new Object();
		}

		String str_et_content = et_content.getEditableText().toString();

		if (StringChecker.isNull(str_et_content)) {
			setError("留言内容不能为空", et_content);
			return false;
		}

		return true;
	}

	ObjTask objTask;

	public void putObj() {
		if (objTask != null) {
			objTask.cancel(true);
		}
		objTask = new ObjTask(this);
		objTask.execute();
	}

	private class ObjTask extends SwipeRefeshAsyncTask<HttpMsg> {

		public ObjTask(Context context) {
			super(context);
		}

		@Override
		public HttpMsg doInBackgroundX(String... params) throws Exception {
			String msg = et_content.getEditableText().toString();
			return AccountService.getService().advise(msg);
		}

		@Override
		public void onPostExecuteX(HttpMsg result) {
			if (result == null) {
				return;
			} else {
				obj = result;
				getActivity().finish();
				// Snackbar.make(this, "成功", 1).show();
			}
		}
	}

	Object obj;

	public void setData(Object obj) {
		this.obj = obj;
		tv_title.setText(null);
		et_content.setText(null);
		et_content.setSelection(et_content.length());
	}

	private void loadData() {
		
	}

	@Override
	public void onDestroy() {

		Cleaner.cleanTask(objTask);
		super.onDestroy();
	}

}